package com.neuedu.his.controller;


import com.neuedu.his.entity.Role;
import com.neuedu.his.entity.User;
import com.neuedu.his.entity.UserRole;
import com.neuedu.his.framework.PageInfo;
import com.neuedu.his.framework.annotation.MethodAlias;
import com.neuedu.his.framework.servlet.BaseServlet;
import com.neuedu.his.service.UserService;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;

import java.util.*;

/**
 * 项目：      his
 * 类名:       UserController
 * 创建时间：  2020/12/22  11:05
 * 描述 :
 * 作者 :     张金山
 * QQ :     314649444
 * Site:      https://jshand.gitee.io
 */
@WebServlet(name = "UserController", urlPatterns = "/user")
public class UserController extends BaseServlet {

    private UserService userService = new UserService();


    /**
     * 根据科室 、 挂号级别查询医生
     * @param request
     * @param response
     * @throws IOException
     */
    public void queryDoctor(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<User> doctorList = userService.queryDoctor(request);
        this.success(response, doctorList);
    }



    // http://127.0.0.1:8080/his/user?type=query  携带身份信息
    @MethodAlias("query")
    public void list(HttpServletRequest request, HttpServletResponse response) throws IOException {

        PageInfo pageInfo = userService.queryListByPage(request);
        //将结果以json格式返回
        this.success(response, pageInfo);
    }


    //http://127.0.0.1:8080/his/user?type=add
    public void save(HttpServletRequest request, HttpServletResponse response) throws ParseException, NoSuchFieldException, IllegalAccessException, InstantiationException, IOException {
        System.out.println("add");

        User user = parseParameter(request, User.class);

        boolean success = false;
        if (user.getUserId() != null) { //修改
            success = userService.update(user);
        } else { //添加
            success = userService.save(user);
        }

        super.success(response, success);
    }

    public void toEdit(HttpServletRequest request, HttpServletResponse response) throws ParseException, NoSuchFieldException, IllegalAccessException, InstantiationException, IOException {

        String id = request.getParameter("userId");

        User user = userService.queryById(Integer.parseInt(id));

        super.success(response, user);
    }


    public void del(HttpServletRequest request, HttpServletResponse response) throws ParseException, NoSuchFieldException, IllegalAccessException, InstantiationException, IOException {

        String id = request.getParameter("userId");

        boolean success = userService.deleteById(Integer.parseInt(id));

        super.success(response, success);
    }

    //user?type=grantRole
    public void grantRole(HttpServletRequest request, HttpServletResponse response) throws ParseException, NoSuchFieldException, IllegalAccessException, InstantiationException, IOException {
        boolean success = userService.grantRole(request);

        super.success(response, success);
    }
    //user?type=userRoles
    public void userRoles(HttpServletRequest request, HttpServletResponse response) throws ParseException, NoSuchFieldException, IllegalAccessException, InstantiationException, IOException {
        List<UserRole> userRoles = userService.getUserRols(request);

        super.success(response, userRoles);
    }


}